//
//  ViewController.m
//  Example13-2
//
//  Created by 孙晓晔 on 2017/4/25.
//  Copyright © 2017年 孙晓晔. All rights reserved.
//

#import "ViewController.h"
#import "DownloadOperation.h"
#import "Download.h"

@interface ViewController ()<DownloadDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@property(nonatomic,strong) NSDictionary *imageDict;
@property(nonatomic,assign) int currentPage;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    NSArray *arr = @[@"http://pic32.photophoto.cn/20140723/0016028127548336_b.jpg", @"http://pic9.photophoto.cn/20081224/0011024043560328_b.jpg", @"http://pic.qiantucdn.com/58pic/11/84/44/79K58PICWak.jpg",@"http://n.sinaimg.cn/news/20170425/P-HV-fyepsch3269218.jpg", @"http://n.sinaimg.cn/news/1_img/upload/c4b46437/20170413/NORK-fyecrxv5796295.jpg"];
    
    Download *download = [[Download alloc]initWithUrl:arr];
    
    download.delegate = self;
    
    [download start:1];
    
    
    self.currentPage = 0;
}

- (void)showImage {
    
    NSArray *keys = _imageDict.allKeys;
    
    NSString *key = keys[_currentPage];
    
    self.imageView.image = _imageDict[key];
}

- (IBAction)prev:(id)sender {
    self.currentPage--;
    if (self.currentPage < 0) {
        self.currentPage = (int)[_imageDict count] - 1;
    }
    
    [self showImage];
}

- (IBAction)next:(id)sender {
    self.currentPage++;
    if (self.currentPage > [_imageDict count] - 1) {
        self.currentPage = 0;
    }
    
    [self showImage];
}

-(void)didFinishDownload:(NSDictionary *)dict {
    NSLog(@"全部已下载 = %ld", [dict count]);
    
    self.imageDict = dict;
    
    [self showImage];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
